package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.UserOrderRecords;
import com.example.mall.dto.UserOrderRecordsDto;
import com.example.mall.mapper.UserOrderRecordsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.UserOrderRecordsQuery;
import com.example.mall.service.IUserOrderRecordsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户下单记录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class UserOrderRecordsServiceImpl extends ServiceImpl<UserOrderRecordsMapper, UserOrderRecords> implements IUserOrderRecordsService {

    @Autowired
    private UserOrderRecordsMapper userOrderRecordsMapper;

    @Override
    public PageDTO<UserOrderRecordsDto> queryPage(UserOrderRecordsQuery userOrderRecordsQuery) {
        if (userOrderRecordsQuery == null) {
            userOrderRecordsQuery = new UserOrderRecordsQuery();
        }
        Page<UserOrderRecords> page = userOrderRecordsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<UserOrderRecords> cartPage = lambdaQuery()
                .like(userOrderRecordsQuery.getOrderId()!= null, UserOrderRecords::getOrderId, userOrderRecordsQuery.getOrderId())
                .like(userOrderRecordsQuery.getUserId()!= null, UserOrderRecords::getUserId, userOrderRecordsQuery.getUserId())
                .like(userOrderRecordsQuery.getRecordId()!= null, UserOrderRecords::getRecordId, userOrderRecordsQuery.getRecordId())
                .like(userOrderRecordsQuery.getIpAddress()!= null, UserOrderRecords::getIpAddress, userOrderRecordsQuery.getIpAddress())
                .like(userOrderRecordsQuery.getDeviceId()!= null, UserOrderRecords::getDeviceId, userOrderRecordsQuery.getDeviceId())
                .orderByDesc(UserOrderRecords::getCreatedDate)
                .page(page);
        return PageDTO.of(cartPage, UserOrderRecordsDto.class);
    }

    @Override
    public UserOrderRecords queryUserOrderRecordsById(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        return userOrderRecordsMapper.selectById(id);
    }

    @Override
    public List<UserOrderRecords> queryUserOrderRecordsList() {
        return this.list();
    }

    @Override
    public void insert(UserOrderRecordsDto userOrderRecordsDto) {
        UserOrderRecords userOrderRecords = new UserOrderRecords();
        CopyUtils.copyProperties(userOrderRecordsDto,userOrderRecords);
        userOrderRecords.setRecordId(CodeGenerateUtils.generateUniqueId());
        userOrderRecords.setCreatedDate(new Date());
        userOrderRecordsMapper.insert(userOrderRecords);
    }

    @Override
    public void update(UserOrderRecordsDto userOrderRecordsDto) {
        AssertUtil.isNotNull(userOrderRecordsDto.getRecordId(),"唯一标识不能为空");
        UserOrderRecords userOrderRecords = userOrderRecordsMapper.selectById(userOrderRecordsDto.getRecordId());
        AssertUtil.isNotNull(userOrderRecords,"该记录不存在");
        CopyUtils.copyProperties(userOrderRecordsDto,userOrderRecords);
        userOrderRecordsMapper.updateById(userOrderRecords);
    }

    @Override
    public void delete(Long id) {
        AssertUtil.isNotNull(id,"唯一标识不能为空");
        UserOrderRecords userOrderRecords = userOrderRecordsMapper.selectById(id);
        AssertUtil.isNotNull(userOrderRecords,"该记录不存在");
        userOrderRecordsMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        AssertUtil.isNotNull(ids,"唯一标识不能为空");
        for (Long id : ids) {
            UserOrderRecords userOrderRecords = userOrderRecordsMapper.selectById(id);
            AssertUtil.isNotNull(userOrderRecords,"该记录不存在");
            userOrderRecordsMapper.deleteById(id);
        }
    }
}
